package com.namo.cchadmin.controller;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.namo.cchadmin.service.AdminService;

import com.namo.cchcommon.api.CommonResult;
import com.namo.cchcommon.api.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author WangXin
 * @date 2021/5/17 20:06
 */
@RestController
@RequestMapping("/user")
public class AdminController {

    @Autowired
    Gson gson;

    @Autowired
    AdminService adminService;

    /**
     * 用户登录
     * @param req
     * @return
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public CommonResult login(@RequestBody String req){
        JsonObject reqObject = gson.fromJson(req, JsonObject.class);
        //参数不对的时候
        if (reqObject.size()!=4){
            return CommonResult.failed(ResultCode.VALIDATE_FAILED);
        }
        String username = reqObject.get("username").getAsString();
        String password = reqObject.get("password").getAsString();
        String captchaCode = reqObject.get("captchaCode").getAsString();
        String captchaId = reqObject.get("captchaId").getAsString();
        return CommonResult.success(adminService.login(username,password,captchaId,captchaCode));
    }
}
